package com.mediacloud.app.newsmodule.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mediacloud.app.assembly.views.CornerBlurView;
import com.mediacloud.app.assembly.widget.EmbedRecyclerView;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.PayTipsUtilsKt;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.utils.GridSpacingItemDecoration;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.tencent.mmkv.MMKV;

/**
 * Created by 陈凯 on 2017/9/21.
 */

public class RelatedVideoGrid extends BaseRelatedView {


    EmbedRecyclerView mRecyclerView;


    RelatedAdapter mRelatedAdapter;
    private Context context;

    public RelatedVideoGrid(Context context) {
        super(context);
        this.context=context;
    }

    public RelatedVideoGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    public RelatedVideoGrid(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RelatedVideoGrid(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context=context;
    }

    @Override
    public void initView() {

        super.initView();

        mRecyclerView = mView.findViewById(R.id.mRecyclerView);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
        mRelatedAdapter = new RelatedAdapter();
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, mContext.getResources().getDimensionPixelSize(R.dimen.dimen13), false));
        mRecyclerView.setAdapter(mRelatedAdapter);

    }

    @Override
    public void setExpandStyle() {
        if (header.getTitleNameText()!=null)
        header.getTitleNameText().setTextColor(Color.parseColor("#FFD43D3D"));
    }

    @Override
    int getViewLayOutId() {
        return R.layout.related_video_grid;
    }

    @Override
    void onNetDataSuccess(ArticleListData data) {
        mRelatedAdapter.notifyDataSetChanged();

    }

    @Override
    void onNetDataFailure(Object data) {

    }

    class RelatedAdapter extends RecyclerView.Adapter<RelatedAdapter.VideoViewHolder> {
        LayoutInflater mInflater;

        public RelatedAdapter() {
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            VideoViewHolder holder = new VideoViewHolder(mInflater.inflate(R.layout.item_related_video, parent, false));


            return holder;
        }

        @Override
        public void onBindViewHolder(VideoViewHolder holder, int position) {
            holder.load(listArticles.get(position));
        }


        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public int getItemCount() {


            if (listArticles != null) {

                Log.e("test", "listArticles grid size()?:" + listArticles.size());
                return listArticles.size();
            }

            return 0;
        }


        class VideoViewHolder extends RecyclerView.ViewHolder {
            CornerBlurView bills;
            TextView tx_times;
            TextView tx_title;
            View mItemView;
            ImageView vrTag;

            public VideoViewHolder(View itemView) {
                super(itemView);
                mItemView = itemView;
                bills = itemView.findViewById(R.id.bills);
                tx_times = itemView.findViewById(R.id.tx_times);
                tx_title = itemView.findViewById(R.id.tx_title);
                vrTag = itemView.findViewById(R.id.vrTag);

            }

            public void load(final ArticleItem data) {
                bills.load(data.getLogo(), true);//,AppFactoryGlobalConfig.getAppServerConfigInfo(itemView.getContext()).isRound());
                Drawable drawableAudio = ContextCompat.getDrawable(tx_times.getContext(), R.drawable.icon_image_model_audio);
                Drawable drawableVideo = ContextCompat.getDrawable(tx_times.getContext(), R.drawable.icon_image_model_video);

                if (data.getType() == TypeX.NEWS.AUDIO) {
                    tx_times.setVisibility(View.VISIBLE);
                    tx_times.setCompoundDrawablesWithIntrinsicBounds(drawableAudio, null, null, null);
                } else if (data.getType() == TypeX.NEWS.VIDEO) {
                    tx_times.setVisibility(View.VISIBLE);
                    tx_times.setCompoundDrawablesWithIntrinsicBounds(drawableVideo, null, null, null);

                } else
                    tx_times.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(data.getProp4())) {
                    tx_times.setText(data.getProp4()+"");
                }else{
                    tx_times.setText("00:00");
                }
//                tx_title.setText(data.getTitle() + "");
                PayTipsUtilsKt.payTips(tx_title, itemView.getResources().getText(R.string.pay_a_pei), data, itemView.getResources().getDimensionPixelSize(R.dimen.pay_label_size), itemView.getResources().getDimensionPixelSize(R.dimen.pay_label_bgradius));
                if (MMKV.defaultMMKV().getBoolean(data.getId() + "", false)) {
                    tx_title.setTextColor(Color.parseColor("#666666"));
                } else {
                    tx_title.setTextColor(Color.parseColor("#222222"));
                }
                if (data.getType() == TypeX.NEWS.VR_VOD || data.getType() == TypeX.NEWS.VR_LIVE) {
                    if (vrTag != null) {
                        vrTag.setVisibility(View.VISIBLE);
                        GradientDrawable gd = (GradientDrawable) vrTag.getBackground();
                        gd.setColor(DefaultBgUtil.getTintColor(getContext()));
                        vrTag.setBackground(gd);
                        if (data.getType() == TypeX.NEWS.VR_VOD) {
                            vrTag.setImageResource(R.drawable.vr_tag_ico);
                        } else {
                            vrTag.setImageResource(R.drawable.vr_tag_live);
                        }
                    }
                } else {
                    if (vrTag != null) {
                        vrTag.setVisibility(View.GONE);
                    }
                }
                mItemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (tx_title != null) tx_title.setTextColor(Color.parseColor("#666666"));
                        NewsItemClickUtils.OpenItemNewsHandle(getContext(), data.getType(), data, new CatalogItem());
                        if (context!=null&& context instanceof Activity){
                            ((Activity)context).finish();
                        }
                    }
                });
            }
        }

    }
}